package com.lrs.java.dataStructure.stack_queue;


/**
 * 规则：数字大代表优先级高
 * @author Michael
 *
 */
class PriorityQueue {
	
	private long[] queueArray;
	private int nElems;
	
	public PriorityQueue(int capacity) {
		queueArray = new long[capacity];
		nElems = 0;
	}
	
	public void insert(long elem) {
		int j;

		if(nElems == 0) {
			queueArray[nElems++] = elem;
		} else{
			for (j = nElems - 1; j >= 0; j--) {
				if (queueArray[j] > elem) {
					queueArray[j + 1] = queueArray[j];
				} else {
					break;
				}
			}
			queueArray[j + 1] = elem;
			nElems++;
		}
	}
	
	public long remove() {
		return queueArray[--nElems];
	}
	
	public long peekMax() {
		return queueArray[nElems-1];
	}
	
	public boolean isFull() {
		return nElems == queueArray.length-1;
	}
	
	public boolean isEmpty() {
		return nElems == 0;
	}
	
	public int size() {
		return nElems;
	}
}


class PriorityQueueApp {
	
	public static void main(String[] args) {
		
		PriorityQueue queue = new PriorityQueue(10);

		queue.insert(21);
		queue.insert(34);
		queue.insert(13);
		queue.insert(45);
		queue.insert(78);
		queue.insert(63);
		
		System.out.println("size: " + queue.size());
		
		while(!queue.isEmpty()) {
			long data = queue.remove();
			System.out.print(data + ", ");
		}
		
	}
}
